package com.tyk.bookstore.back.payment.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.payment.manager.excel.constant.ExcelConstant;
import com.tyk.bookstore.back.payment.manager.excel.model.ShopStatementExcelVo;
import com.tyk.bookstore.back.payment.model.query.ShopStatementAddQuery;
import com.tyk.bookstore.back.payment.model.query.ShopStatementPageQuery;
import com.tyk.bookstore.back.payment.model.query.ShopStatementUpdateQuery;
import com.tyk.bookstore.back.payment.model.vo.ShopStatementPageVo;
import com.tyk.bookstore.back.payment.model.vo.ShopStatementVo;
import com.tyk.bookstore.back.payment.service.ShopStatementService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author tyk
 * @description 书店流水
 */
@Tag(name = "支付管理/书店流水管理", description = "书店流水管理")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/payment/shop-statement")
public class ShopStatementController {

    private final ShopStatementService shopStatementService;
    private final ExcelManager<ShopStatementExcelVo> excelManager;

    @Operation(summary = "查询书店流水详情", description = "查询书店流水详情")
    @SaCheckPermission("payment:shop-statement:get")
    @GetMapping("{id}")
    public R<ShopStatementVo> getById(
            @Schema(description = "书店流水ID")
            @NotNull @Min(value = 1)
            @PathVariable Long id
    ) {
        return R.ok(shopStatementService.getById(id));
    }

    @Operation(summary = "分页查询书店流水", description = "分页查询书店流水")
    @SaCheckPermission("payment:shop-statement:page")
    @GetMapping("page")
    public R<ShopStatementPageVo> getPage(
            @Schema(description = "书店流水分页查询参数")
            @ParameterObject
            @Validated ShopStatementPageQuery query
    ) {
        return R.ok(shopStatementService.getPage(query));
    }

    @Operation(summary = "添加书店流水", description = "添加书店流水")
    @SaCheckPermission("payment:shop-statement:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody ShopStatementAddQuery query
    ) {
        shopStatementService.add(query);
        return R.ok();
    }

    @Operation(summary = "更新书店流水", description = "更新书店流水")
    @SaCheckPermission("payment:shop-statement:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody ShopStatementUpdateQuery query
    ) {
        shopStatementService.updateById(query);
        return R.ok();
    }

    @Operation(summary = "删除书店流水", description = "删除书店流水")
    @SaCheckPermission("payment:shop-statement:delete")
    @DeleteMapping("{id}")
    public R<Object> deleteById(
            @Schema(description = "书店流水ID")
            @Min(value = 1) @NotNull
            @PathVariable Long id
    ) {
        shopStatementService.deleteById(id);
        return R.ok();
    }

    @Operation(summary = "批量删除书店流水", description = "批量删除书店流水")
    @SaCheckPermission("payment:shop-statement:delete")
    @DeleteMapping
    public R<Object> deleteBatchByIdSet(
            @Schema(description = "书店流水ID列表")
            @NotEmpty @NotNull Set<Long> idList
    ) {
        shopStatementService.deleteBatchByIdSet(idList);
        return R.ok();
    }

    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("payment:shop-statement:excel:download")
    @GetMapping("excel")
    public void downloadExcel(ShopStatementPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                shopStatementService.getShopStatementExcelVoList(query),
                ExcelConstant.SHOP_STATEMENT_FILE_NAME,
                ExcelConstant.SHOP_STATEMENT_SHEET,
                resp
        );
    }
}

